package com.oscarmovie.action;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.oscarmovie.Constants;
import com.oscarmovie.model.Award;
import com.oscarmovie.model.Calendar;
import com.oscarmovie.model.Category;
import com.oscarmovie.model.Country;
import com.oscarmovie.model.Movie;
import com.oscarmovie.model.Paging;
import com.oscarmovie.model.Person;
import com.oscarmovie.model.Role;
import com.oscarmovie.model.Statistics;
import com.oscarmovie.service.AwardService;
import com.oscarmovie.service.CacheService;
import com.oscarmovie.service.CalendarService;
import com.oscarmovie.service.CategoryService;
import com.oscarmovie.service.CountryService;
import com.oscarmovie.service.MovieService;
import com.oscarmovie.service.PersonService;
import com.oscarmovie.service.RoleService;

public class IndexAction extends ActionSupport{
	private AwardService awardService;
	private CalendarService calendarService;
	private CategoryService categoryService;
	private PersonService personService;
	private CountryService countryService;
	private MovieService movieService;
	private RoleService roleService;

	private CacheService cacheService;
	
	private static final long serialVersionUID = 1L;

	
	//only need getters
	private List<Award> awards;
	private List<Calendar> calendars;
	private List<Category> categories;
	private List<Country> countries;
	private List<Role> roles;
	
	//For search
	private List<Person> persons;
	private List<Movie> movies;
	
	
	
	private int calendarId;
	private int categoryId;
	private int countryId;
	private int personId;
	private int awardId;
	private int type;
	
	private Paging paging = new Paging(10);
	
	/**************************************************************************/
	public String execute() throws Exception {
		awards = awardService.loadAll();
		countries = countryService.loadAll();
		calendars = calendarService.loadAll();
		categories = categoryService.loadAll();
		cacheService.loadAllMovieCache();
		roles = roleService.loadAll();
		
		return SUCCESS;
	}
	public String about() throws Exception {
		return Constants.ABOUT;
	}
	public String news() throws Exception {
		return Constants.NEWS;
	}
	/**RANKING************************************************************************/
	private List<Movie> mostAwardedMovies;
	private List<Movie> mostScoredMovies;
	private List<Movie> mostWatchedMovies;
	private List<Movie> mostWantedMovies;
	private List<Person> mostAwardedDirectors;
	private List<Person> mostAwardedActors;
	private List<Person> mostPopularDirectors;
	private List<Person> mostPopularActors;
	private List<Person> mostScoredDirectors;
	private List<Person> mostScoredActors;
	public List<Movie> getMostAwardedMovies() {
		return mostAwardedMovies;
	}
	public void setMostAwardedMovies(List<Movie> mostAwaredMovies) {
		this.mostAwardedMovies = mostAwaredMovies;
	}
	public List<Movie> getMostScoredMovies() {
		return mostScoredMovies;
	}
	public void setMostScoredMovies(List<Movie> mostScoredMovies) {
		this.mostScoredMovies = mostScoredMovies;
	}
	public List<Movie> getMostWatchedMovies() {
		return mostWatchedMovies;
	}
	public void setMostWatchedMovies(List<Movie> mostWatchedMovies) {
		this.mostWatchedMovies = mostWatchedMovies;
	}
	public List<Movie> getMostWantedMovies() {
		return mostWantedMovies;
	}
	public void setMostWantedMovies(List<Movie> mostWantedMovies) {
		this.mostWantedMovies = mostWantedMovies;
	}
	public List<Person> getMostAwardedDirectors() {
		return mostAwardedDirectors;
	}
	public void setMostAwardedDirectors(List<Person> mostAwaredDirectors) {
		this.mostAwardedDirectors = mostAwaredDirectors;
	}
	public List<Person> getMostAwardedActors() {
		return mostAwardedActors;
	}
	public void setMostAwardedActors(List<Person> mostAwardedActors) {
		this.mostAwardedActors = mostAwardedActors;
	}
	public List<Person> getMostPopularDirectors() {
		return mostPopularDirectors;
	}
	public void setMostPopularDirectors(List<Person> mostPopularDirectors) {
		this.mostPopularDirectors = mostPopularDirectors;
	}
	public List<Person> getMostPopularActors() {
		return mostPopularActors;
	}
	public void setMostPopularActors(List<Person> mostPopularActors) {
		this.mostPopularActors = mostPopularActors;
	}
	public void setAwards(List<Award> awards) {
		this.awards = awards;
	}
	public void setMostScoredActors(List<Person> mostScoredActors) {
		this.mostScoredActors = mostScoredActors;
	}
	public List<Person> getMostScoredActors() {
		return mostScoredActors;
	}
	public void setMostScoredDirectors(List<Person> mostScoredDirectors) {
		this.mostScoredDirectors = mostScoredDirectors;
	}
	public List<Person> getMostScoredDirectors() {
		return mostScoredDirectors;
	}
	public String ranking() throws Exception {
		int count = 10;
		mostAwardedMovies = movieService.loadMostAwarded(count);
		mostScoredMovies = movieService.loadMostScore(count);
		mostWatchedMovies = movieService.loadMostWatched(count);
		mostWantedMovies = movieService.loadMostWanted(count);
		
		mostAwardedDirectors = personService.loadMostAwarded(count);
		mostAwardedActors = personService.loadMostAwarded(count);
		mostScoredDirectors = personService.loadMostScore(count);
		mostScoredActors = personService.loadMostScore(count);
		mostPopularDirectors = personService.loadMostFavorite(count);
		mostPopularActors = personService.loadMostFavorite(count);
		return Constants.RANKING;
	}
	private Movie createAwaredMovie(int id, String name, String nameCn, int awarded) {
		Movie movie = new Movie();
		Statistics statistics = new Statistics();
		movie.setStatistics(statistics);
		
		movie.setId(id);
		movie.setName(name);
		movie.setNameCn(nameCn);
		statistics.setAwarded(awarded);
		
		return movie;
	}
	
	/**PEOPLE***********************************************************************/
	public String people() throws Exception {
		persons = personService.loadAll();
		return Constants.PEOPLE;
	}
	
	/**SEARCH***********************************************************************/
	private String key;
	public String search() throws Exception {
		if(key!=null && !key.equals("")){
			key = new String(key.getBytes("ISO8859-1"), "UTF-8");
			
			Movie m = new Movie();
			key = key.trim();
			m.setName(key);
			m.setNameCn(key);
			movies = movieService.list(paging,m);
			
			Person p = new Person();
			p.setName(key);
			p.setNameCn(key);
			persons = personService.list(paging,p);
			
			key = null;
		} else {
			movies = null;
			persons = null;
		}
		return Constants.SEARCH;
	}
	
	/**AJAX: category***********************************************************************/
	public void category() throws Exception{
		if(categories==null) categories = categoryService.loadAll();
		
	}
	/**AJAX: calendar***********************************************************************/
	public String calendar() throws Exception{
		if(calendars==null) calendars = calendarService.loadAll();
		return Constants.CALENDAR;
	}
	/**AJAX: award***********************************************************************/
	public String award() throws Exception{
		if(awards==null) awards = awardService.loadAll();
		return Constants.AWARD;
	}
	/**AJAX: country***********************************************************************/
	public String country() throws Exception{
		if(countries==null) countries = countryService.loadAll();
		return Constants.COUNTRY;
	}
	/**AJAX: role***********************************************************************/
	public String role(){
		if(roles==null) roles = roleService.loadAll();
		return Constants.ROLE;
	}
	
	
	/**
	 * @param cacheService the cacheService to set
	 */
	public void setCacheService(CacheService cacheService) {
		this.cacheService = cacheService;
	}

	public AwardService getAwardService() {
		return awardService;
	}



	public void setAwardService(AwardService awardService) {
		this.awardService = awardService;
	}



	public CalendarService getCalendarService() {
		return calendarService;
	}



	public void setCalendarService(CalendarService calendarService) {
		this.calendarService = calendarService;
	}



	public CategoryService getCategoryService() {
		return categoryService;
	}



	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}



	public PersonService getPersonService() {
		return personService;
	}



	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}



	public void setCountryService(CountryService countryService) {
		this.countryService = countryService;
	}



	public List<Award> getAwards() {
		return awards;
	}


	public List<Calendar> getCalendars() {
		return calendars;
	}


	public List<Category> getCategories() {
		return categories;
	}


	public List<Person> getPersons() {
		return persons;
	}


	public List<Country> getCountries() {
		return countries;
	}



	public static long getSerialversionuid() {
		return serialVersionUID;
	}



	



	public void setMovieService(MovieService movieService) {
		this.movieService = movieService;
	}



	public MovieService getMovieService() {
		return movieService;
	}



	public List<Movie> getMovies() {
		return movies;
	}



	public void setCalendarId(int calendarId) {
		this.calendarId = calendarId;
	}



	public int getCalendarId() {
		return calendarId;
	}



	public void setCategoryId(int categoryId) {
		this.categoryId = categoryId;
	}



	public int getCategoryId() {
		return categoryId;
	}



	public void setCountryId(int countryId) {
		this.countryId = countryId;
	}



	public int getCountryId() {
		return countryId;
	}



	public void setPersonId(int personId) {
		this.personId = personId;
	}



	public int getPersonId() {
		return personId;
	}



	public void setAwardId(int awardId) {
		this.awardId = awardId;
	}



	public int getAwardId() {
		return awardId;
	}



	public void setPaging(Paging paging) {
		this.paging = paging;
	}



	public Paging getPaging() {
		return paging;
	}

	public void setType(int type) {
		this.type = type;
	}

	public int getType() {
		return type;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getKey() {
		return key;
	}
	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}
	public List<Role> getRoles() {
		return roles;
	}
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}
	public RoleService getRoleService() {
		return roleService;
	}
	
	
}
